package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import pojo.Orders;
import pojo.User;
import service.UserManager;
import service.impl.UserManagerFactory;
import util.DButil;



/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserManager manager;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        manager=UserManagerFactory.getInstance();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String uname=request.getParameter("uname");
		String upsd=request.getParameter("upsd");
		
	    Set<Orders> oSet=new HashSet<Orders>();
	    request.getSession().setAttribute("oset", oSet);
	       
		DataSource ds=(DataSource)request.getServletContext().getAttribute("ds");
		Connection conn=DButil.getConnection(ds);
		User u= manager.getUser(uname, conn);
		DButil.close(conn);
		if (u!=null) {
			Logger logger=Logger.getLogger(LoginServlet.class);
			logger.debug(uname+"d登錄了");
			request.getSession().setAttribute("user", u);
			request.getRequestDispatcher("show").forward(request, response);
		}else {
			request.setAttribute("error", "登录错误，重新登录！");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		
		
		
		
		

		
	
	}

}
